#ifndef IOT_H_
#define IOT_H_
#include "stdint.h"

// ring buffer size
#define RING_BUFFER_LEN     (1024u)

// The length of a single buffer used by DMA
#define RX_BUFFER_LEN       (128u)

typedef struct USART_DMA_CTRL_st
{
    volatile uint8_t DMA_USE_BUFFER;
    uint8_t          Rx_Buffer[2][RX_BUFFER_LEN];
} USART_DMA_CTRL;

typedef struct RING_BUFFER_st
{
    uint8_t           buffer[RING_BUFFER_LEN];
    volatile uint16_t RecvPos;  //
    volatile uint16_t SendPos;  //
    volatile uint16_t RemainCount;
} RING_BUFFER;

typedef struct LINE_BUFFER_st
{
    char* pbuf;
    int16_t len;
} LINE_BUFFER;

typedef struct TOPIC_BUFFER_st
{
    uint16_t link_id_;
    char* topic_;   // 以'\0'结尾
    char* data_;    // 以'\0'结尾
    // uint16_t data_len_;
} TOPIC_BUFFER;

typedef struct ROUTE_ITEM_st
{
    const char *keyword_;
    void (*callback)(const char *param, uint16_t len);
} ROUTE_ITEM;

//===============waiting_OK=================
typedef struct WAIT_OK_st
{
    uint8_t is_waiting_;    // pdTRUE
    uint32_t tick_;
    uint16_t timeout_ms_;
    int result_;            // 1.收到OK;
} WAIT_OK;

//===============waiting_OK=================

// depercated
typedef struct at_agent_st {
    // k_chr_fifo_t    uart_rx_fifo;
    uint8_t        *uart_rx_fifo_buffer;
    uint32_t        uart_rx_fifo_len;
    uint32_t        uart_rx_fifo_len_max;
} at_agent_t;

void ring_buffer_push_huge(uint8_t *buffer, uint16_t len);
uint8_t ring_buffer_pop();
uint8_t ring_buffer_peek();

void iot_hal_init();

void DMA_INIT(void);

void reset_wait_OK(WAIT_OK *pw);
int waiting_OK(int timeout_ms);

void xy_at_uart_input_byte(at_agent_t *at_agent, uint8_t data);
int get_relay_val_from_str(const char* str);

void iot_task_entry(void *pvParameters);
void iot_monitor_task_entry(void *pvParameters);

#endif // IOT_H_
